﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ClassGenerator;
using Pangoo.Common;
using Sirenix.OdinInspector;

namespace MetaTable
{
    public class CSharpCodeDataWriter : CsharpCodeWriterBase
    {
        List<MetaTableColumn> m_Columns;



        public CSharpCodeDataWriter(List<string> headers, List<MetaTableColumn> columns)
        {
            m_Columns = columns;
            m_Headers = headers;
        }

        public override void WriteMainClassStart(IJsonClassGeneratorConfig config, TextWriter sw)
        {
            sw.WriteLine();
            sw.WriteLine("namespace {0}", config.Namespace);
            sw.WriteLine("{");
            sw.WriteLine("    [Serializable]");

            sw.WriteLine("    {0} partial class {1} : {2}", "public", JsonClassGenerator.ToTitleCase(config.MainClass), config.BaseClass);
            sw.WriteLine("    {");
        }



        public (int, MetaTableColumn) GetColumnByName(string name)
        {
            foreach (var col in m_Columns)
            {
                if (col.Name == name)
                {
                    return (m_Columns.IndexOf(col), col);
                }
            }
            return (-1, null);
        }


        public override void WriteClassMembers(IJsonClassGeneratorConfig config, TextWriter sw, JsonType type, string prefix)
        {
            foreach (var field in type.Fields)
            {

                if (config.BaseFields != null && config.BaseFields.Contains(field.MemberName))
                {
                    continue;
                }

                var (col_index, col) = GetColumnByName(field.MemberName);


                if (config.UsePascalCase || config.ExamplesInDocumentation)
                    sw.WriteLine();

                if (col.IsEditor)
                {
                    sw.WriteLine("#if UNITY_EDITOR");
                }

                if (config.ExamplesInDocumentation)
                {
                    sw.WriteLine(prefix + "/// <summary>");
                    sw.WriteLine(prefix + "/// Examples: " + field.GetExamplesText());
                    sw.WriteLine(prefix + "/// </summary>");
                }

                if (col.IsEditor)
                {
                    sw.WriteLine(prefix + "[JsonNoMember]");
                    sw.WriteLine(prefix + "[XmlIgnore]");
                    sw.WriteLine(prefix + "[NonSerialized]");
                }
                else
                {
                    sw.WriteLine(prefix + "[JsonMember(\"{0}\")]", field.MemberName);
                }
                if (col != null)
                {
                    if (!col.IsEditor)
                    {
                        sw.WriteLine(prefix + $"[MetaTableRowColumn(\"{field.MemberName}\",\"{col.Type}\", \"{col.CnName}\",{col_index + 1})]");
                    }
                    sw.WriteLine(prefix + "[LabelText(\"{0}\")]", col.CnName);
                }

                if (col.Type.ToLower().Equals("color"))
                {
                    sw.WriteLine(prefix + "[ColorUsage(true, true)]");
                }

                if (col.Attributes != null)
                {
                    foreach (var attr in col.Attributes)
                    {
                        if (attr.IsNullOrWhiteSpace()) continue;
                        var instance = ClassUtility.CreateInstance(attr) as MetaTableAttribute;
                        sw.WriteLine(prefix + "[" + (instance == null ? attr : instance.Attribute) + "]");
                    }
                }

                //使用模板Example值作为类型
                //export_path不作为类型导出
                if (config.ExamplesToType && field.Type.Type == JsonTypeEnum.String && field.JsonMemberName != "@export_path")
                    sw.WriteLine(prefix + "public {0} {1};", GetTypeFromExample(field.GetExamplesText()), field.MemberName);
                else
                    sw.WriteLine(prefix + "public {0} {1};", field.Type.GetTypeName(), field.MemberName);



                if (col.Attributes != null)
                {
                    bool hasAttrFunciton = false;
                    foreach (var attr in col.Attributes)
                    {
                        if (attr.IsNullOrWhiteSpace()) continue;
                        var instance = ClassUtility.CreateInstance(attr) as MetaTableAttribute;
                        if (instance != null)
                        {
                            hasAttrFunciton = true;
                            sw.WriteLine("");
                            sw.WriteLine("#if UNITY_EDITOR");
                            instance.AttributeFunction(sw, prefix);
                        }
                    }

                    if (hasAttrFunciton)
                    {
                        sw.WriteLine("");
                        sw.WriteLine("#endif");
                    }


                }


                if (col.IsEditor)
                {
                    sw.WriteLine("#endif");
                }
            }
        }
    }
}